﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;

namespace SPPermissions.GetListPermissions
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                string siteURL;
                string username;
                string password;
                string ListName;
                Console.Write("Enter Site URL :");
                siteURL = Console.ReadLine();
                Console.Write("Enter User Name (Domain\\UserName) :");
                username = Console.ReadLine();
                Console.Write("Enter Password :");
                password = Console.ReadLine();
                Console.Write("Enter List Title :");
                ListName = Console.ReadLine();
                Console.WriteLine("Loading the permissions...");

                ClientContext ctx = new ClientContext(siteURL);

                ctx.Credentials = new System.Net.NetworkCredential(username.Split('\\')[1], password, username.Split('\\')[0]);
                Web webSite = ctx.Web;
                List list = webSite.Lists.GetByTitle(ListName);

                //Following Load method loads,
                //Line 2: Identify the website has Unique Perimission or Inherited Permission
                //Line 3: Get the List Title Property
                //Line 4: Get the collection of RoleAssignment, associated to the Site
                //Get the Member and (collection of RoleDefinition) RoleDefinitionBindingCollection property from RoleAssignment
                //Line 5,6: Member Property retrives the Title and Id
                //Line 7,8: RoleDefintion retrives the Title Property

                //Start: Line 0
                ctx.Load(list, olist =>
                    olist.HasUniqueRoleAssignments,
                    olist => olist.Title,
                    olist => olist.RoleAssignments.Include(
                        roleAssignment => roleAssignment.Member.Title,
                        roleAssignment => roleAssignment.Member.Id,
                        roleAssignment => roleAssignment.RoleDefinitionBindings.Include(
                            roleDefinition => roleDefinition.Name)));
                //End: Line 9

                ctx.ExecuteQuery();

                //Code for Output Follows
                RoleAssignmentCollection roleAssCollection = list.RoleAssignments;
                Console.WriteLine("List Title: " + list.Title);
                Console.WriteLine("List has Unique Permissions: " + list.HasUniqueRoleAssignments);
                Console.WriteLine("Total Role Assignments: " + roleAssCollection.Count.ToString());
                Console.WriteLine("Group Name \t Group ID \t Permission Level\n**********************************************************");
                foreach (RoleAssignment roleAss in roleAssCollection)
                {
                    Principal roleMember = roleAss.Member;
                    Console.Write(roleMember.Title + " \t " + roleMember.Id + " \t ");
                    foreach (RoleDefinition roleDef in roleAss.RoleDefinitionBindings)
                    {
                        Console.Write(roleDef.Name + ";");
                    }
                    Console.WriteLine("");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.Write("\nPress any key to Exit..");
            Console.Read();
        }
    }
}
